package top.lidee.common.service.twice.applet.impl;

import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import top.lidee.common.enums.StatusEnum;
import top.lidee.common.service.StoreService;
import top.lidee.common.service.twice.applet.MtStoreCateService;
import top.lidee.common.service.twice.applet.StoreIndexService;
import top.lidee.framework.result.PageUtils;
import top.lidee.framework.result.ResponseResult;
import top.lidee.module.backendApi.twice.page.Query;
import top.lidee.repository.mapper.MtGoodsMapper;
import top.lidee.repository.model.MtGoods;
import top.lidee.repository.model.MtMerchant;
import top.lidee.repository.model.MtStore;
import top.lidee.repository.model.twice.pojo.applet.MtStoreCateEntity;
import top.lidee.repository.model.twice.vo.applet.StoreIndexVo;
import top.lidee.utils.CopyBean;
import top.lidee.utils.MyCollUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static top.lidee.utils.MapParamsUtil.getKey;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/10/22 14:33
 */
@Service("storeIndexService")
public class StoreIndexServiceImpl implements StoreIndexService {

    @Resource
    private StoreService storeService;

    @Resource
    private MtStoreCateService mtStoreCateService;

    @Resource
    private MtGoodsMapper mtGoodsMapper;

    /**
     * @param params
     * @return top.lidee.framework.result.ResponseResult
     * @description 根据分类id查询店铺信息 分页
     * @author zhangyiheng
     * @date 2024/10/22 14:34
     */
    @Override
    public ResponseResult conditionalQueries(Map<String, Object> params) {

        Integer cateId = (Integer) params.get("cateId");

        List<MtStoreCateEntity> cates = mtStoreCateService.list(Wrappers.<MtStoreCateEntity>lambdaQuery()
                .eq(MtStoreCateEntity::getParentId, cateId));

        LambdaQueryWrapper<MtStore> wrapper = Wrappers.<MtStore>lambdaQuery()
                .in(MtStore::getCateId, cates.stream().map(MtStoreCateEntity::getCategoryId).collect(Collectors.toList()));

        return getResponseResult(params, wrapper);
    }

    private ResponseResult getResponseResult(Map<String, Object> params, LambdaQueryWrapper<MtStore> wrapper) {
        IPage<MtStore> page = storeService.page(
                new Query<MtStore>().getPage(params),
                wrapper
        );

        List<MtStore> records = page.getRecords();

        if (MyCollUtil.isNotEmpty(records)) {
            List<StoreIndexVo> storeIndexVos = CopyBean.copyBeanList(records, StoreIndexVo.class);

            storeIndexVos.forEach(store -> {
                MtStoreCateEntity cate = mtStoreCateService.getById(store.getCateId());
                store.setCateName(cate.getName());
            });

            IPage<StoreIndexVo> pageVo = new Page<>();
            pageVo.setTotal(page.getTotal());
            pageVo.setPages(page.getPages());
            pageVo.setCurrent(page.getCurrent());
            pageVo.setRecords(storeIndexVos);
            pageVo.setSize(storeIndexVos.size());
            return ResponseResult.okResultPage("page", new PageUtils(pageVo));
        }
        return ResponseResult.okResultPage("page", new PageUtils(page));
    }


    /**
     * @param params
     * @return top.lidee.framework.result.ResponseResult
     * @description 搜索栏：根据店铺标题(名)/地址,模糊搜索分页
     * @author zhangyiheng
     * @date 2024/10/22 14:51
     */
    @Override
    public ResponseResult conditionalLikeQueries(Map<String, Object> params) {
        String key = getKey(params);

        LambdaQueryWrapper<MtStore> wrapper = Wrappers.<MtStore>lambdaQuery()
                .like(StrUtil.isNotBlank(key), MtStore::getName, key)
                .or().like(StrUtil.isNotBlank(key), MtStore::getLicenseName, key)
                .or().like(StrUtil.isNotBlank(key), MtStore::getAddress, key);


        return getResponseResult(params, wrapper);
    }

    /**
     * @param storeId
     * @return top.lidee.framework.result.ResponseResult
     * @description 点击店铺，查询商品
     * @author zhangyiheng
     * @date 2024/10/22 15:15
     */
    @Override
    public ResponseResult getInfoAShopListByMerchantsId(Integer storeId) {
        //查询店铺下商品
        List<MtGoods> list = mtGoodsMapper.selectList(Wrappers.<MtGoods>lambdaQuery()
                .eq(MtGoods::getStoreId, storeId)
                .eq(MtGoods::getStatus, "A"));

        return ResponseResult.okResult(list);
    }
}
